/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.tkgarment.system.dao;

import com.tkgarment.system.domain.Province;
import com.tkgarment.system.factory.DAOFactory;
import com.tkgarment.system.inf.IDAO;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author zraj
 */
public class ProvinceDAO extends AbstractDAO implements IDAO<Province, Integer> {

    @Override
    public List<Province> getAll() throws Exception {
        String sql = "select  * from province order by "+ Province.field_province_name +" ASC";
        if (this.getCon() == null) {
            this.setCon(DAOFactory.creatConnection());
        }
        
        List<Map<String , Object>> result = this.getCon().queryList(sql);
        List<Province> provinces = new ArrayList<>();
        for (Map<String, Object> map : result) {
            Province p = new Province();
            p.setProvinceID(Integer.parseInt(map.get(Province.field_province_id).toString()));
            p.setProvinceName(map.get(Province.field_province_name).toString());
            provinces.add(p);
        }
        return provinces;
    }
    

    @Override
    public int updateSingle(Province t, Integer id) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int deleteSingle(Province t) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int insertSingle(Province t) throws Exception {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Province getById(Integer id) throws Exception {
         String sql = "select  * from province where " + Province.field_province_id +" = ?";
        if (this.getCon() == null) {
            this.setCon(DAOFactory.creatConnection());
        }
        
        Map<String , Object> map = this.getCon().querySingle(sql,id);
        
       
            Province p = new Province();
            p.setProvinceID(Integer.parseInt(map.get(Province.field_province_id).toString()));
            p.setProvinceName(map.get(Province.field_province_name).toString());
           
        return p;
    }
    
}
